package com.zouqone.smc.common.utils.http;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class PostClient {
	/**
	 * 文件投递地址
	 */
	private String uri = "";

	public PostClient(String uri) {
		this.uri = uri;
	}

	/**
	 * 投递文件
	 * 
	 * @param filePath
	 *            投递文件的本地路径
	 * @return 是否执行成功
	 * @throws IOException
	 */
	public boolean post(String filePath) throws IOException {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost request = new HttpPost(uri);

		FileEntity entity = new FileEntity(new File(filePath));
		request.setEntity(entity);

		CloseableHttpResponse response = null;
		try {
			response = client.execute(request);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return false;
		}

		// 获取响应内容
		if (response != null)
			try {
				printStreamContent(response.getEntity().getContent());
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		return true;
	}

	/**
	 * 输出流内容
	 * 
	 * @param is
	 */
	private void printStreamContent(InputStream is) {
		BufferedReader br = null;

		try {
			br = new BufferedReader(new InputStreamReader(is));
			String line = "";
			StringBuffer sb = new StringBuffer("");
			while ((line = br.readLine()) != null) {
				sb.append(line);
				sb.append("\n");
				// TODO: 如果需要,打开注释,将输出响应内容
				// System.out.println(sb.toString());
			}

			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取文件内容
	 * 
	 * @param filePath
	 * @return 文件内容
	 * @throws IOException
	 */
	private String getFileContent(String filePath) throws IOException {
		File file = new File(filePath);
		BufferedReader br = new BufferedReader(new FileReader(file));

		String line = "";
		StringBuilder sb = new StringBuilder();
		while ((line = br.readLine()) != null) {
			sb.append(line);
			sb.append("\n");
		}
		br.close();

		return sb.toString();
	}

	public boolean post(List<String> filePathList) throws IOException {
		if (filePathList == null)
			throw new NullPointerException();

		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost request = new HttpPost(uri);
		List<BasicNameValuePair> formParams = new ArrayList<BasicNameValuePair>();

		// 处理每一个文件
		for (String filePath : filePathList) {
			String fileContent = getFileContent(filePath);
			BasicNameValuePair pair = new BasicNameValuePair(filePath, fileContent);
			formParams.add(pair);
		}

		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
		request.setEntity(entity);

		try {
			client.execute(request);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public static void main(String args[]) {
		String uri = "http://localhost:8080/upload.json";
		PostClient client = new PostClient(uri);

		try {
			// TODO: 修改测试文件地址
//			client.post("D:/index.ftl");
			List<String> list = new ArrayList<String>();
			list.add("D:/index.ftl");
			list.add("D:/coupon.ftl");
			client.post(list);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
